﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DATA
{
    public class AdministratorREP
    {
        MODELGENERALDataContext context = new MODELGENERALDataContext();

        public AdministratorDTO Login(string uname, string pword)
        {
            try
            {
                Administrator ad = context.Administrators.Single(a => a.adminID == uname && a.password == pword);
                AdministratorDTO admin = new AdministratorDTO();
                admin.AdminID = ad.adminID;
                admin.RoleID = ad.roleID;
                return admin;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<AdministratorDTO> GetAllAdministrators()
        {
            List<AdministratorDTO> acc = new List<AdministratorDTO>();

            var acounts = from a in context.Administrators
                          select a;
            AdministratorDTO adminDTO;
            foreach (var account in acounts)
            {
                adminDTO = new AdministratorDTO();
                adminDTO.AdminID = account.adminID;
                adminDTO.Password = account.password;
                adminDTO.RoleID = account.roleID;
                acc.Add(adminDTO);
            }
            return acc;
        }

        public bool AddAdministrator(AdministratorDTO newAccount)
        {
            try
            {
                Administrator admin = new Administrator();
                admin.adminID = newAccount.AdminID;
                admin.password = newAccount.Password;
                admin.roleID = 2;
                context.Administrators.InsertOnSubmit(admin);
                context.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
            }
            return false;

        }
        public void deleteAdministrator(string AccountID)
        {
            var acc = context.Administrators.Single(a => a.adminID == AccountID);
            context.Administrators.DeleteOnSubmit(acc);
            context.SubmitChanges();


        }

        public bool UpdateAdministrator(AdministratorDTO accountup)
        {
            try
            {
                Administrator acc = context.Administrators.Single(
                                                        nv => nv.adminID == accountup.AdminID
                                                        );
                acc.password = accountup.Password;
                acc.roleID = accountup.RoleID;
                context.SubmitChanges();
            }

            catch (System.Exception ex)
            {
                return false;
            }
            return true;
        }
    }
}
